<?php
include_once(dirname(__FILE__) . "/model/nms_alarm_model.php");
include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
include_once(dirname(__FILE__) . "/model/nms_interface_model.php");
include_once(dirname(__FILE__) . "/libs/config.php");
function draw_group_interface_manage(){
	html_master_script_filter();
	html_group_interface_script_reload();
	html_group_interface_manage_script();
	draw_group_interface_manage_header();
	$sql_where1  ="";
	if (!empty($_GET['group_name'])) {
		$sql_where1 .="and b.interface_group_id =".$_GET['group_name']."";
	}else{
		$sql_where1 .="and b.interface_group_id =0";
	}
	if (!empty($_REQUEST['search'])) {
		$research=trim($_REQUEST['search']);
		$sql_where1.=" and (CONCAT('(',c.interface_name,')','\t',c.interface_alias) like '%".$research."%')";
	}else{
		$_REQUEST['search']="";
	}
	$query1 = "SELECT count(b.interface_id)
								FROM plugin_nms_groupname a, plugin_nms_interfacegroup b ,plugin_nms_interface c
								where a.interface_group_id = b.interface_group_id  and b.interface_id =c.interface_id  ".$sql_where1."";
	$display1 =db_fetch_cell($query1);
	$querydata1="SELECT b.intergroup_id as id,  a.group_name ,CONCAT('(',c.interface_name,')','\t',c.interface_alias) as interface_des ,CONCAT_WS('',d.description,' (',d.hostname,')') as host_name
								FROM plugin_nms_groupname a, plugin_nms_interfacegroup b, plugin_nms_interface c ,host d
								where  a.interface_group_id = b.interface_group_id and b.interface_id =c.interface_id and c.host_id =d.id ".$sql_where1."
									order by d.id ,b.interface_id ,Convert(SUBSTRING(c.interface_name,3,1),UNSIGNED INTEGER),Convert(SUBSTRING(c.interface_name,5,2),UNSIGNED INTEGER) limit 0,$display1";
	if (empty($_REQUEST['group_name'])) {
		$group_interface="";
	}
	else{
		$group_interface =  $_REQUEST['group_name'];
	}
	$url1= "nms.php?run=group&tab=group_view&group_name=".$group_interface."&search=".$_REQUEST['search']."";
	draw_group_interface_manage_table($url1,$querydata1);
	if (isset($_GET['search'])) {
		?>
						<script >
							var tmp = $('#id_master_table tr').removeHighlight().highlight('<?php echo trim($_GET['search']) ?>');
							alert(test);
							$('#id_master_table').empty();
							$('#id_master_table').html(test);
						</script>
	<?php 
		}
	?>
	   	<script type="text/javascript">
			   $("#id_group").val('<?php echo $group_interface;?>');
		</script>
	<?php 
	if(isset($_REQUEST['action'])){
		if($_REQUEST['action'] == 'delete'){
			$id =(int) $_REQUEST['id_del'];
			$interface_id = db_fetch_cell("SELECT interface_id from plugin_nms_interfacegroup where intergroup_id =".$id."");
			$interface_group_id=db_fetch_cell("SELECT interface_group_id from plugin_nms_interfacegroup where intergroup_id =".$id."");
			if($interface_group_id==1){
				echo'<script>
					alert("Default Group not remove !!!");
					</script>';	
			}else{
			interface_to_group(1, $interface_id);
			remove_interface_to_group($id);
			}
			echo '<script>
					 			reLoad();
					 		</script>';
		}
	}
	if (empty($_GET['group_name'])) {
		$group_name1="";
	}
	else{
		$group_name1 =  $_GET['group_name'];
	}
	if (empty($_GET['search'])) {
		$id_search="";
	}
	else{
		$id_search =  $_GET['search'];
	}
	$url_post= "nms.php?run=group&tab=group_view&group_name=".$group_name1."&search=".$id_search."";
	draw_group_interface_manage_footer($url_post);
 if(isset($_POST['btnAdd']))
 {
 	$group_name =$_POST['id_add_group'];
 	if(empty($_POST['id_add_group'])){
 		echo'
						<script>
								alert("Group_interface not null!");
						</script>';
 	}else{
 		if(check_add_group_interface($group_name)==true){
 			echo'
						<script>
								alert("Group_interface already exists!");
						</script>';
 		}else{
 			add_group_interface($group_name);
 		}}
 		echo '<script>
		 			reLoad();
		 		</script>';
 }
 if(isset($_POST['btnDel']))
 {
 	$id = $_POST['id_group_inter'];
 	
 	if(empty($_POST['id_add_group'])){
		$result = db_fetch_assoc("SELECT interface_id from plugin_nms_interfacegroup where interface_group_id=".$id."");
		foreach ($result as $set){
			remove_interface_to_group_1($set['interface_id'],$id);
			interface_to_group(1,$set['interface_id']);
		}
 		remove_group_interface($id);
 	}else{
 			
 	}
 	echo '<script>
		 			reLoad();
		 		</script>';
 }
}
?>
<?php 
	function draw_group_interface_manage_header(){
		html_master_header_start("Group Interface Management");
		?>
    				<table width="50%" border="0">
			    	    <tr>
			    		<td >Group Interface:</td>
		    			<td >
		    			<select  onchange="val()" id="id_group">
			        		<?php
	     					 $hosts = db_fetch_assoc("select interface_group_id,group_name from plugin_nms_groupname order by group_name");     
	     					 if (sizeof($hosts) > 0) {
						     foreach($hosts as $set) {
						     echo "<option value='" . $set["interface_group_id"] . "'"; echo ">" . htmlspecialchars($set["group_name"]) . "</option>\n";
						     }
						     }
	   						?>
		        		</select>
		    		</td>
					     <td>Search</td>
							<td colspan="3" scope="col">
	    					<input id="id_search" type="search" onkeypress="keydown(event)"/>
	    			</td>
					</tr>
					</table>
		<?php 
		html_master_header_end();
	}
	function draw_group_interface_manage_table($url1,$querydata1){
				html_master_table_start("100%","center")
					?>
							<th width="5%" scope="col">Id</th>
							<th width="20%" scope="col">Group_interface</th>
							<th width="20%" scope="col">Host_name</th>
							<th width="60%" scope="col">Interface</th>
							<th width="10%" scope="col"></th>
						</tr>
					<?php 
					$result_group = db_fetch_assoc($querydata1);
					$stt=1;
					foreach($result_group as $set1) {
					?>
					<tr <?php if($stt%2==0) {echo 'class="xanhlot"';}
				 					else {echo 'class="xanhhong"';} ?>> 
					 	<td align="center">&nbsp;<?php echo $stt++ ?></td>
					<td align="center">&nbsp;<?php echo $set1['group_name']?></td>
					<td align="center">&nbsp;<?php echo $set1['host_name']?></td>
					<td align="center">&nbsp;<?php echo $set1['interface_des']?></td>
					<td align="center"><a href="<?php echo $url1 ?>&action=delete&id_del=<?php echo $set1['id'];?>"onclick="return confirm('Are you sure you want to delete :<?php echo $set1['interface_des']?>?')"><?php echo'Delete';?></a></td>
					</tr>
					<?php 
					}
   			html_master_table_end();
	}
	
	function draw_group_interface_manage_footer($url_post){
		?>		
				<div id="id_master_header">
				</div>
				<form action="<?php echo $url_post ?>" method ="post"enctype="multipart/form-data" id ="id_check">
				<div id ="id_footer_group">
				<table width="50%" border="0">
			    	<tr>
					<td >Group Interface:</td>
		    		<td >
		    			<select  onchange="val()" name="id_group_inter">
		    				<option value=""></option>
			        		<?php
	     					 $hosts = db_fetch_assoc("select interface_group_id,group_name from plugin_nms_groupname where interface_group_id != 1 order by group_name");     
	     					 if (sizeof($hosts) > 0) {
						     foreach($hosts as $set) {
						     echo "<option value='" . $set["interface_group_id"] . "'"; echo ">" . htmlspecialchars($set["group_name"]) . "</option>\n";
						     }
						     }
	   						?>
		        		</select>
		    		</td>
		    		<td><input type="submit" name="btnDel" id ="btnDel" value="Del"   onclick="return confirm('Are you sure you want to delete host?')"/></td>
					<td   colspan="3" scope="col">
				    		<input name="id_add_group" type="text" />
				    </td>
		    		<td><input type="submit" name="btnAdd" id= "btnAdd" value="Add" /></td>
		    		<td>
		    			Refresh time:	 			
		    		</td>
		    		<td>
		    			<h4><div id ="countdown_time2"></div></h4>
		    		</td>
		    		</tr>
		    		
		    	</table>
		    	</div>
			</form>
			</div>
				  	 	 			
			</div>
<?php
		html_alarm_script_time();
	}
	
?>